var readTable={
    convertClassName:function(val) {
        if (val == null || val == "")
            return;
        var valArray = val.split("_");
        val = "";
        $(valArray).each(function (i, str) {
            val += str.substring(0, 1).toUpperCase();
            val += str.substring(1, str.length).toLowerCase();
        });
        return val;
    },
    convertFieldName:function(val) {
        if (val == null || val == "")
            return;
        var valArray = val.split("_");
        val = "";
        $(valArray).each(function (i, str) {
            if(i==0){
                val += str.toLowerCase();
            }else{
                val += str.substring(0, 1).toUpperCase();
                val += str.substring(1, str.length).toLowerCase();
            }
        });
        return val;
    },
    read:function(sql){
        var data={};

        var sql = sql.toLowerCase();
        sql = sql.replace(/`/g, "");
        var zIndex = sql.indexOf("(");
        var yIndex = sql.lastIndexOf(")");
        var header = sql.substring(0, zIndex);
        var lastSql = sql.substring(yIndex, sql.length) ? sql.substring(yIndex, sql.length) : "";
        sql = sql.substring(zIndex + 1, yIndex);
        if (header.indexOf("create table") != -1) {
            header = header.split("create table");
        }
        if (header.length > 0) {
            data.tableName=header[1].trim();
           var  className = readTable.convertClassName(header[1].trim());
            data.className=className;
        }

        var tableDesc = null;
        var tableDescIndex = lastSql.indexOf("comment='");
        if (tableDescIndex != -1) {
            tableDesc = lastSql.substring(tableDescIndex + "comment='".length, lastSql.lastIndexOf("'"));
            data.desc=tableDesc;
        } else {
            tableDescIndex = lastSql.indexOf("comment '");
            if (tableDescIndex != -1) {
                tableDesc = lastSql.substring(tableDescIndex + "comment '".length, lastSql.lastIndexOf("'"));
                data.desc=tableDesc;
            }
        }
        sql = sql.split("\n");
        data.fieldList=[];
        var classId={};
        var notNull={};
        $(sql).each(function (i, val) {
            if (val == null || val == "") {
                return true;
            }
            if (val.indexOf("primary key") != -1) {
                var tableId =readTable.convertFieldName(val.substring(val.indexOf("(") + 1, val.lastIndexOf(")")));
                classId[tableId]=true;
                return true;
            }
            if (val.indexOf(" key ") != -1&&val.indexOf("`key`") == -1) {
                return true;
            }

            if(val.indexOf(" unique key ")!=-1&&val.indexOf("`unique key`"==-1)){
                return true;
            }
            val = val.trim();
            var child = {};
            child.descInfo = "";
            if (val.indexOf("comment") != -1 && val.indexOf("'") != -1) {
                var contentVal = val.split("comment");
                child.descInfo = contentVal[1].substring(contentVal[1].indexOf("'") + 1, contentVal[1].lastIndexOf("'"));
                if(child.descInfo!=""){
                    var index= child.descInfo.indexOf("(");
                    var index1= child.descInfo.indexOf(":");
                    if(index==-1&&index1==-1){
                        child.desc=child.descInfo
                    }else if(index==-1){
                        child.desc=child.descInfo.substring(0,index1);
                    }else if(index1==-1){
                        child.desc=child.descInfo.substring(0,index);
                    }else{
                        if(index>index1){
                            index=index1;
                        }
                        child.desc=child.descInfo.substring(0,index);
                    }
                }
            }

            if(val.indexOf("not null")!=-1){
                child.notNull=true;
            }else{
                child.notNull=false;
            }

            var sqlArray = [];
            if (val.indexOf("'") == -1) {
                sqlArray = val.split(" ")
            } else {
                sqlArray = val.substring(0, val.indexOf("'")).split(" ")
            }
            $(sqlArray).each(function (k, ival) {
                if (ival != "" && ival != null) {
                    if (k == 0) {
                        child.name =readTable.convertFieldName(ival);
                        child.column=ival;
                    } else if (k == 1) {
                        var type = null;
                        if (ival.indexOf("(") != -1) {
                            type = ival.substring(0, ival.indexOf("("));
                        } else {
                            type = ival
                        }
                        child.type =readTable.mysqlTypeMap[type.toUpperCase()];
                    }
                }
            });
            data.fieldList.push(child);
        });
        for(var i=0;i<data.fieldList.length;i++){
            if(classId[data.fieldList[i].name]!=null){
                data.fieldList[i].id=true;
                data.fieldList[i].notNull=true;
            }
        }
        return data;
    },
    mysqlTypeMap:{
        "LONGTEXT":"String",
        "VARCHAR" : "String",
        "CHAR" : "String",
        "BLOB" : "byte[]",
        "VARBINARY" : "byte[]",
        "TEXT" : "String",
        "INTEGER" : "Long",
        "INT" : "Integer",
        "TINYINT" : "Integer",
        "SMALLINT" : "Integer",
        "MEDIUMINT" : "Integer",
        "BIT" : "Boolean",
        "BIGINT" : "BigInteger",
        "FLOAT" : "FLOAT",
        "DOUBLE" : "Double",
        "DECIMAL" : "BigDecimal",
        "BOOLEAN" : "Integer",
        "ID" : "Long",
        "DATE" : "Date",
        "TIME" : "Date",
        "DATETIME" : "Date",
        "YEAR" : "Date",
        "TIMESTAMP" : "Date"
    }

};